/** 
 ** Copyright (c) 2010 Ushahidi Inc
 ** All rights reserved
 ** Contact: team@ushahidi.com
 ** Website: http://www.ushahidi.com
 ** 
 ** GNU Lesser General Public License Usage
 ** This file may be used under the terms of the GNU Lesser
 ** General Public License version 3 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.LGPL included in the
 ** packaging of this file. Please review the following information to
 ** ensure the GNU Lesser General Public License version 3 requirements
 ** will be met: http://www.gnu.org/licenses/lgpl.html.	
 **	
 **
 ** If you have questions regarding the use of this file, please contact
 ** Ushahidi developers at team@ushahidi.com.
 ** 
 **/

package com.ushahidi.android.app.net;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpResponseException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.ushahidi.android.app.UshahidiPref;

public class AroundMeNetRequest
{
    public static JSONArray getNearByResourceFromWeb(double d, boolean inMile, double latitude, double longitude)
    {
	StringBuilder uriBuilder = new StringBuilder(UshahidiPref.domain);
	if (!UshahidiPref.domain.endsWith("/"))
	    uriBuilder.append("/");
	uriBuilder.append("api?task=resources&by=near&d="+d);
	
	if(inMile)
	    uriBuilder.append("&dunit=mi");
	else
	    uriBuilder.append("&dunit=km");
	
	uriBuilder.append("&lat="+latitude+"&lon="+longitude);
	
	//api?task=resources&by=near&d=2&dunit=mi&lat=34.04469&lon=-118.24310&resp=xml
	
	String url = uriBuilder.toString();
	String resource = "";
	HttpResponse response = null;

	try
	{
	    response = UshahidiHttpClient.GetURL(url);
	}
	catch (HttpResponseException e)
	{
	    System.err.println(e.toString());
	}
	catch(Exception e)
	{
	    
	    System.err.println(e.toString());
	}
	Log.v("Oceanmaster", "Request:[" + url + "]");

	if (response == null)
	{
	    return null;
	}

	final int statusCode = response.getStatusLine().getStatusCode();

	if (statusCode == 200)
	{
	    resource = UshahidiHttpClient.GetText(response).toString();
	    Log.v("Oceanmaster", "resource Result:[" + resource + "]");
	    
	    try
            {
	        JSONArray result = new JSONObject(resource).getJSONObject("payload").getJSONArray("resources");
	        return result;
            }
            catch (JSONException e)
            {
        	Log.v("Oceanmaster", "Error occured during parsing JSONObject!!");
	        return null;
            }
	}
	else
	{
	    return null;
	}
    }
    public static JSONArray getNearByResourceFromWeb(double d, boolean inMile, double latitude, double longitude,String catid)
    {
	StringBuilder uriBuilder = new StringBuilder(UshahidiPref.domain);
	if (!UshahidiPref.domain.endsWith("/"))
	    uriBuilder.append("/");
	uriBuilder.append("api?task=resources&by=near&d="+d);
	
	if(inMile)
	    uriBuilder.append("&dunit=mi");
	else
	    uriBuilder.append("&dunit=km");
	
	uriBuilder.append("&lat="+latitude+"&lon="+longitude+"&c="+catid);
	
	//api?task=resources&by=near&d=2&dunit=mi&lat=34.04469&lon=-118.24310&resp=xml
	
	String url = uriBuilder.toString();
	String resource = "";
	HttpResponse response = null;

	try
	{
	    response = UshahidiHttpClient.GetURL(url);
	}
	catch (HttpResponseException e)
	{
	    System.err.println(e.toString());
	}
	catch(Exception e)
	{
	    
	    System.err.println(e.toString());
	}
	Log.v("Oceanmaster", "Request:[" + url + "]");

	if (response == null)
	{
	    return null;
	}

	final int statusCode = response.getStatusLine().getStatusCode();

	if (statusCode == 200)
	{
	    resource = UshahidiHttpClient.GetText(response).toString();
	    Log.v("Oceanmaster", "resource Result:[" + resource + "]");
	    
	    try
            {
	        JSONArray result = new JSONObject(resource).getJSONObject("payload").getJSONArray("resources");
	        return result;
            }
            catch (JSONException e)
            {
        	Log.v("Oceanmaster", "Error occured during parsing JSONObject!!");
	        return null;
            }
	}
	else
	{
	    return null;
	}
    }
	public static boolean getNearByResourceFromWeb_XML(double d, boolean inMile, double latitude, double longitude,String catid) {
		StringBuilder uriBuilder = new StringBuilder(UshahidiPref.domain);
		if (!UshahidiPref.domain.endsWith("/"))
		    uriBuilder.append("/");
		uriBuilder.append("api?task=resources&by=near&d="+d);
		
		if(inMile)
		    uriBuilder.append("&dunit=mi");
		else
		    uriBuilder.append("&dunit=km");
		
		uriBuilder.append("&lat="+latitude+"&lon="+longitude+"&c="+catid+"&resp=xml");
		
		//api?task=resources&by=near&d=2&dunit=mi&lat=34.04469&lon=-118.24310&resp=xml
		
		String url = uriBuilder.toString();
		String resource = "";
		HttpResponse response = null;

		try
		{
		    response = UshahidiHttpClient.GetURL(url);
		}
		catch (HttpResponseException e)
		{
		    System.err.println(e.toString());
		}
		catch(Exception e)
		{
		    
		    System.err.println(e.toString());
		}
		Log.v("Oceanmaster", "Request:[" + url + "]");

		if (response == null)
		{
		    return false;
		}
		final int statusCode = response.getStatusLine().getStatusCode();

		if (statusCode == 200)
		{
			resource = UshahidiHttpClient.GetText(response);
            UshahidiPref.resourceResponse = resource;
            return true;
		}
		else
		{
		    return false;
		}
	}
	public static boolean getNearByResourceFromWeb_XML(double d, boolean inMile, double latitude, double longitude) {

		StringBuilder uriBuilder = new StringBuilder(UshahidiPref.domain);
		if (!UshahidiPref.domain.endsWith("/"))
		    uriBuilder.append("/");
		uriBuilder.append("api?task=resources&by=near&d="+d);
		
		if(inMile)
		    uriBuilder.append("&dunit=mi");
		else
		    uriBuilder.append("&dunit=km");
		
		uriBuilder.append("&lat="+latitude+"&lon="+longitude+"&resp=xml");
		
		//api?task=resources&by=near&d=2&dunit=mi&lat=34.04469&lon=-118.24310&resp=xml
		
		String url = uriBuilder.toString();
		String resource = "";
		HttpResponse response = null;

		try
		{
		    response = UshahidiHttpClient.GetURL(url);
		}
		catch (HttpResponseException e)
		{
		    System.err.println(e.toString());
		}
		catch(Exception e)
		{
		    
		    System.err.println(e.toString());
		}
		Log.v("Oceanmaster", "Request:[" + url + "]");

		if (response == null)
		{
		    return false;
		}
		final int statusCode = response.getStatusLine().getStatusCode();

		if (statusCode == 200)
		{
			resource = UshahidiHttpClient.GetText(response);
            UshahidiPref.resourceResponse = resource;
            return true;
		}
		else
		{
		    return false;
		}

	}
}
